//
//  XMGPictureTopicView.m
//  BuDeJie19
//
//  Created by hjj on 2016/12/3.
//  Copyright © 2016年 hjjuny. All rights reserved.
//

#import "XMGPictureTopicView.h"
#import "XMGTopicItem.h"
#import <UIImageView+WebCache.h>
#import <DALabeledCircularProgressView.h>
#import "XMGSeeBigPictureViewController.h"
@interface XMGPictureTopicView ()
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
@property (weak, nonatomic) IBOutlet UIImageView *pictureView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigButton;
@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *progressView;

@end

@implementation XMGPictureTopicView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    _progressView.progressTintColor = [UIColor whiteColor];
    _progressView.trackTintColor = [UIColor lightGrayColor];
    _progressView.roundedCorners = 5;
    
    _progressView.progressLabel.textColor = [UIColor whiteColor];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"点击了图片");
    // 弹出查看大图View
    XMGSeeBigPictureViewController *seeVc = [[XMGSeeBigPictureViewController alloc] init];
    seeVc.item = self.item;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:seeVc animated:YES completion:nil];
    
}

- (void)setItem:(XMGTopicItem *)item
{
    [super setItem:item];
    
    _progressView.progress = 0;
    _progressView.progressLabel.text = @"0%";
    NSString *fileName = [item.image2 stringByReplacingOccurrencesOfString:@"/" withString:@""];
    
    // 先从沙盒中获取之前裁剪图片
   
    NSString *filePath = [CachePath stringByAppendingPathComponent:fileName];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    UIImage *image = [UIImage imageWithData:data];
    
    if (image) {
        item.localImage = image;
        _pictureView.image = image;
        
    } else {
        [_pictureView sd_setImageWithURL:[NSURL URLWithString:item.image2] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
            // 进度的Block在子线程调用
            
            if (expectedSize == -1) return ;
            
            CGFloat progress = 1.0 * receivedSize / expectedSize;
            
            NSString *str = [NSString stringWithFormat:@"%.0f%%",progress * 100];
            
            dispatch_sync(dispatch_get_main_queue(), ^{
                _progressView.progressLabel.text = str;
                
                _progressView.progress = progress;
            });
            
        } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { // 加载完图片就会调用
         
            if (image.size.height < 10000) return;
            
            CGFloat imageW = XMGScreenW - 20;
            CGFloat imageH = imageW / image.size.width * image.size.height;
            // 开启图形上下文
            UIGraphicsBeginImageContext(CGSizeMake(imageW, imageH));
            
            // 画图
            [image drawInRect:CGRectMake(0, 0, imageW, imageH)];
            
            // 取图
            image = UIGraphicsGetImageFromCurrentImageContext();
            
            // 关闭上下文
            UIGraphicsEndImageContext();
            // 重新显示到imageView
            _pictureView.image = image;
            
            // 保存到模型中
            item.localImage = image;
            
            // 保存到沙盒中
            // 图片转NSData
            NSString *fileName = [item.image2 stringByReplacingOccurrencesOfString:@"/" withString:@""];
            NSString *filePath = [CachePath stringByAppendingPathComponent:fileName];
            NSData *data = UIImagePNGRepresentation(image);
            BOOL isError = [data writeToFile:filePath atomically:YES];
//            NSLog(@"%d",isError);
            
        }];

    }

    
    _gifView.hidden = !item.is_gif;
    
    // 做大图处理
    _seeBigButton.hidden = !item.is_bigPicture;
    
    if (item.is_bigPicture) {
        // 设置图片内容模式
        _pictureView.contentMode = UIViewContentModeTop;
        _pictureView.clipsToBounds = YES;
    } else {
        _pictureView.contentMode = UIViewContentModeScaleToFill;
        _pictureView.clipsToBounds = NO;
    }
    
}
@end
